//
//  YZLSearchCarViewController.m
//  Car
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLSearchCarViewController.h"
#import "HTTPTOOL.h"
#import "YZLCarBrandModel.h"
#import "YZLTableViewCell.h"
#import "YZLHotCarListModel.h"
#import "YZLHotCarListTableViewCell.h"
#import "YZLCarDetailViewController.h"

@interface YZLSearchCarViewController ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
//车品牌列表
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *keyArr;
@property (nonatomic, strong) NSMutableArray *numArr;
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) UITableView *CarListTableView;
//车型列表
@property (nonatomic, strong) NSMutableArray *subBrandNameArr;
@property (nonatomic, strong) NSMutableArray *seriesNum;
@property (nonatomic, strong) NSMutableArray *listModelArr;
//指示器
@property (nonatomic, strong) UIView *activity;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;

@end

@implementation YZLSearchCarViewController

#pragma mark - 数据处理
- (void)dataHandle {
    
    NSString *str = @"http://mi.xcar.com.cn/interface/xcarapp/getBrands.php";
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [result objectForKey:@"letters"];
        self.keyArr = [NSMutableArray array];
        NSString *string = @"#";
        [self.keyArr addObject:string];
        self.numArr = [NSMutableArray array];
        self.modelArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            
            [self.keyArr addObject:[[dic objectForKey:@"letter"] uppercaseString]];
            [self.numArr addObject:[dic objectForKey:@"brandNum"]];
            YZLCarBrandModel *model = [YZLCarBrandModel CarBrandModelWithDic:dic];
            [self.modelArr addObject:model];
            
        }
       
        [self.tableView reloadData];
        [self.activityView stopAnimating];
        self.activity.hidden = YES;
        
    } fail:^(NSError *error) {
        
    }];
    
}

#pragma mark - 创建tableView
- (void)createTableView {
    //车品牌列表
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //右边索引的背景颜色
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //右边索引的字体颜色
    self.tableView.sectionIndexColor = [UIColor blackColor];
    
    //车型列表
    self.CarListTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 64, self.view.frame.size.width - 45, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.CarListTableView];
    self.CarListTableView.delegate = self;
    self.CarListTableView.dataSource = self;
    self.CarListTableView.rowHeight = 60;
    //去掉分隔线
    UIView *view = [[UIView alloc] init];
    self.CarListTableView.tableFooterView = view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"品牌选车";
    
    //bar颜色
    [self navigationbarColor];
    
    [self createTableView];
    [self dataHandle];
    
    /* 在View上添加手势 */
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(clicked:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    
    /* 指示器 */
    self.activity = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.activity.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.activity];
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityView.color = [UIColor lightGrayColor];
    self.activityView.center = CGPointMake(self.activity.frame.size.width / 2.f, self.activity.frame.size.height / 2.f - 64);
    [self.activity addSubview:self.activityView];
    [self.activityView startAnimating];
}

#pragma mark - tableView 协议方法
//每个section的cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView) {
        
        if (section == 0) {
            
            return 1;
            
        }else {
            
            return [[self.numArr objectAtIndex:section - 1] integerValue];
        }
        
    }else {
        
        return [[self.seriesNum objectAtIndex:section] integerValue];
    }
}
//section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.tableView) {
        
        return self.keyArr.count;
        
    }else {
        
        return self.subBrandNameArr.count;
    }
}
//cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView) {
        
        if (indexPath.section == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuse"];
            }
            cell.textLabel.text = @"热门品牌";
            cell.imageView.image = [UIImage imageNamed:@"hot"];
            return cell;
            
        }else {
            YZLCarBrandModel *model = [self.modelArr objectAtIndex:indexPath.section - 1];
            YZLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (cell == nil) {
                cell = [[YZLTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            cell.num = indexPath.row;
            cell.model = model;
            return cell;
        }
        
    }else {
        
        YZLHotCarListModel *model = [self.listModelArr objectAtIndex:indexPath.section];
        YZLHotCarListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[YZLHotCarListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        }
        cell.listNum = indexPath.row;
        cell.model = model;
        return cell;
    }
}

/* 分区的头部标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (tableView == self.tableView) {
        
        return [self.keyArr objectAtIndex:section];
        
    }else {
        
        return [self.subBrandNameArr objectAtIndex:section];
    }
}

/* 右边的索引 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    if (tableView == self.tableView) {
        
        return self.keyArr;
        
    }else {
        
        return nil;
    }
}

#pragma mark - cell 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == self.tableView) {
        //侧滑弹出车型列表
        [UIView animateWithDuration:0.5 animations:^{
            
            self.CarListTableView.frame = CGRectMake(60, 64, self.view.frame.size.width - 60, self.view.frame.size.height - 113);
            
        }];
        //数据请求
        NSString *str = nil;
        if (indexPath.section == 0) {
            //热门品牌
            str = @"http://mi.xcar.com.cn/interface/xcarapp/getTopSeries.php";
            
        }else {
            //其他车品牌
            YZLCarBrandModel *model = [self.modelArr objectAtIndex:indexPath.section - 1];
            NSDictionary *dic = [model.brands objectAtIndex:indexPath.row];
            NSString *idStr = [dic objectForKey:@"id"];
            str = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getSeriesByBrandId.php?brandId=%@", idStr];
        }
        [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
            
            NSArray *arr = [result objectForKey:@"subBrands"];
            self.listModelArr = [NSMutableArray array];
            self.subBrandNameArr = [NSMutableArray array];
            self.seriesNum = [NSMutableArray array];
            for (NSDictionary *dic in arr) {
                [self.subBrandNameArr addObject:[dic objectForKey:@"subBrandName"]];
                [self.seriesNum addObject:[dic objectForKey:@"seriesNum"]];
                YZLHotCarListModel *model = [YZLHotCarListModel HotCarListModelWithDic:dic];
                [self.listModelArr addObject:model];
            }
            
            [self.CarListTableView reloadData];
            
        } fail:^(NSError *error) {
            
        }];
        
    }else {
        
        [self.CarListTableView  deselectRowAtIndexPath:indexPath animated:YES];
        YZLCarDetailViewController *vc = [[YZLCarDetailViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        YZLHotCarListModel *model = [self.listModelArr objectAtIndex:indexPath.section];
        NSDictionary *dic = [model.series objectAtIndex:indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%@", [dic objectForKey:@"seriesId"]];
        vc.seriesId = str;
    }
}

#pragma mark - 实现UIGestureRecognizerDelegate代理方法 收回tableView
- (void)clicked:(UIPanGestureRecognizer *)pan {
    
    if (pan.state == UIGestureRecognizerStateChanged)
    {
        CGFloat translation = [pan translationInView:self.view].x;
        
        if (translation > 0)
        {
            [UIView animateWithDuration:0.5 animations:^{
                self.CarListTableView.frame = CGRectMake(self.view.frame.size.width, 64, self.view.frame.size.width - 60, self.view.frame.size.height - 113);
            }];
        }
    }
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
